<?php

namespace app\controller\teacher;

use app\BaseController;
use app\model\Discuss;
use app\model\StudentCourse;
use app\model\StudentDiscuss;
use app\model\VStudentCourse;
use Exception;
use mysqli;
use \think\facade\Db;

/**
 * 教师-讨论控制器
 */
class TeacherDiscuss extends BaseController
{

    /**
     * 讨论列表
     */
    public function list()
    {
        $courseID = $this->request->param("courseID");
        $discussList = Discuss::where(["courseID" => $courseID, 'TID' => $this->user['TID']])->select();
        return resultSuccess($discussList);
    }

    /**
     * 添加讨论
     */
    public function addDiscuss()
    {
        $data = $this->request->param();
        $data['discussID'] = null;
        $data['TID'] = $this->user['TID'];
        $discuss = new Discuss(
            $data
        );
        if ($discuss->save()) {
            return resultSuccess();
        } else {
            return resultError();
        }
    }

    /**
     * 删除讨论
     */
    public function deleteDsicuss()
    {
        $discussID = $this->request->param('discussID');
        $discuss = Discuss::where('TID', $this->user['TID'])->find($discussID);
        if ($discuss->delete()) {
            return resultSuccess();
        } else {
            return resultError();
        }
    }

    /**
     * 修改讨论
     */
    public function editDiscuss()
    {
        $data = $this->request->param();
        $discuss = Discuss::where(['TID' => $this->user['TID'], 'discussID' => $data['discussID']])->find();
        if (empty($discuss)) {
            return resultError();
        }
        $discuss->theme = $data['theme'];
        $discuss->credit = $data['credit'];
        if ($discuss->save()) {
            return  resultSuccess();
        } else {
            return resultError();
        }
    }

    /**
     * 讨论详情
     */
    public function discussDetail()
    {
        $discussID = $this->request->param('discussID');
        $discuss = Discuss::where(['TID' => $this->user['TID'], 'discussID' => $discussID])->find();
        if (empty($discuss)) {
            return resultError();
        }
        $teacherQuery = Db::table('teacherdiscuss')->where('discussID', $discussID)->buildSql();
        $discussHistory = StudentDiscuss::union($teacherQuery)->where('discussID', $discussID)->order('updateTime', 'desc')->select()->toArray();
        //历史记录
        $discuss['discussHisroy'] = $discussHistory;
        return resultSuccess($discuss);
    }

    /**
     * 讨论统计
     */
    function countDiscuss()
    {
        $discussID = $this->request->param('discussID');
        $discuss = Discuss::where(['TID' => $this->user['TID'], 'discussID' => $discussID])->find();
        if (empty($discuss)) {
            return resultError();
        }
        $discussCount = StudentDiscuss::field(['name,student.SID' => 'SID', 'date(updateTime)' => 'updateTime'])
            ->where(['discussID' => $discussID])
            ->join('student', 'student.SID = studentdiscuss.sid')
            ->select()->toArray();
        $studentCount = array();
        $dateCount = array();
        foreach ($discussCount as $dis) {
            if (!isset($studentCount[$dis['SID']])) {
                $studentCount[$dis['SID']]['count'] = 1;
                $studentCount[$dis['SID']]['name'] = $dis['name'];
                $studentCount[$dis['SID']]['SID'] = $dis['SID'];
            } else
                $studentCount[$dis['SID']]['count']++;
            if (!isset($dateCount[$dis['updateTime']]))
                $dateCount[$dis['updateTime']] = 1;
            else
                $dateCount[$dis['updateTime']]++;
        }
        usort($studentCount,function($a,$b){return $b['count']-$a['count'];});
        $unFinishStudentList = VStudentCourse::field(['SID', 'studentName', 'claseName'])->where('courseID',$discuss->courseID)
            //->whereNotIn('SID', StudentDiscuss::field('SID')->where('discussID', $discussID)->buildSql())
            ->where('SID', 'not in',function($query) use($discussID){$query->table('studentdiscuss')->field(['SID'])->where('discussID', $discussID);})
            ->select();
        //未完成学生列表
        return resultSuccess(['studentCount' => array_values($studentCount), 'dateCount' => $dateCount, 'unFinishStudentList' => $unFinishStudentList]);
    }
}
